package com.tdkj.task;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.Table;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tdkj.model.risevalue.BeanAdvertAuctionInfo;
import com.tdkj.service.ServiceException;
import com.tdkj.service.risevalue.IBeanAdvertAuctionService;
import com.tdkj.util.typeconverter.DateTypeConverter;

public class AdvertAuctionResultDetermineTask {
	 /** Logger */
    private static final Logger logger = LoggerFactory.getLogger(AdvertAuctionResultDetermineTask.class);
    
    private IBeanAdvertAuctionService advertAuctionService;
 
    @Resource
	public void setAdvertAuctionService(IBeanAdvertAuctionService advertAuctionService) {
		this.advertAuctionService = advertAuctionService;
	}



	/**
     * 业务逻辑处理
     */
	public void execute() {
    	while(true){
    		Long sleepMillis = 2L*60*1000;
    			
    		Date currentDate = new Date(System.currentTimeMillis());
    		String currentDateString = DateTypeConverter.convertToString(currentDate, "yyyy-MM-dd");
    		String sql = "select id from " + BeanAdvertAuctionInfo.class.getAnnotation(Table.class).name() + " where status='开始竞价' and aendtime<='"+currentDateString+"'";
			List<Object> auctions = advertAuctionService.findBySql(sql);
    		
			for(Object object : auctions){
				Long auctionId = ((BigInteger)(object)).longValue();
				try {
					advertAuctionService.endAuction(auctionId);
				} catch (ServiceException e) {
					logger.debug(e.getMessage());
				}
			}
			
    		try {
    			//睡眠时间
				Thread.sleep(sleepMillis);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
    	}
    }
}
